﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Evlution_Generate
{
    public sealed class SqlHelp
    {

        private static object o = new object();
        private static SqlHelp sh;

        private String _conStr;
        public String ConStr
        {
            get { return _conStr; }
            set { _conStr = value; }
        }
        private SqlHelp() { }
        private SqlHelp(String ConStr)
        {
            this.ConStr = ConStr;
        }

        public static SqlHelp CreateInstance(String ConStr)
        {
            if (sh == null)
            {
                lock (o)
                {
                    if (sh == null)
                    {
                        sh = new SqlHelp(ConStr);
                    }
                }
            }
            return sh;
        }


        private SqlConnection GetConn()
        {
            SqlConnection conn = new SqlConnection(ConStr);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return conn;
        }

        /// <summary>
        /// 执行不带参数的SQL查询语句或存储过程
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="ct"></param>
        /// <returns>table</returns>
        public DataTable ExcuteQuery(string cmdText, CommandType ct)
        {
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand(cmdText, GetConn());
            cmd.CommandType = ct;
            SqlDataReader sdr;
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }

        /// <summary>
        ///  执行不带参数的增删改SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">增删改SQL语句或存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns>语句影响行数</returns>
        public int ExecuteNonQuery(string cmdText, CommandType ct)
        {
            int res;
            SqlCommand cmd;
            using (cmd = new SqlCommand(cmdText, GetConn()))
            {
                cmd.CommandType = ct;
                res = cmd.ExecuteNonQuery();
            }
            return res;
        }

        /// <summary>
        /// 重载ExecuteNonQuery
        /// 默认命令类型为Text
        /// </summary>
        /// <param name="cmdText">需要执行的命令</param>
        /// <returns></returns>
        public int ExecuteNonQuery(String cmdText)
        {
            return ExecuteNonQuery(cmdText, CommandType.Text);
        }

        /// <summary>
        /// 重载ExcuteQuery
        /// 默认命令类型为Text
        /// </summary>
        /// <param name="cmdText">需要执行的命令</param>
        /// <returns></returns>
        public DataTable ExcuteQuery(string cmdText)
        {
            return ExcuteQuery(cmdText, CommandType.Text);
        }


    }
}
